# tag/views.py
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status

from management.middleware import skip_middleware_user_activity
from utils.logging_utils import log_request_response
from .models import Tag, AlgorithmTag
from .serializers import TagSerializer, TagSerializer_for_liu, TagSerializer_for_liu2


# 获取所有的标签
@api_view(['GET'])
@skip_middleware_user_activity
@log_request_response
def get_all_tags(request):
    tags = Tag.objects.all()
    serializer = TagSerializer(tags, many=True)
    return Response(serializer.data)

@api_view(['GET'])
@skip_middleware_user_activity
@log_request_response
def get_all_algorithm_tags(request):
    tags = AlgorithmTag.objects.all()
    serializer = TagSerializer(tags, many=True)
    return Response(serializer.data)


@api_view(['GET'])
@skip_middleware_user_activity
@log_request_response
def get_all_algorithm_tags_for_liu(request):
    tags = AlgorithmTag.objects.all()
    serializer = TagSerializer_for_liu(tags, many=True)
    return Response(serializer.data)

@api_view(['GET'])
@skip_middleware_user_activity
@log_request_response
def get_all_algorithm_tags_for_liu2(request):
    tags = AlgorithmTag.objects.all()
    serializer = TagSerializer_for_liu2(tags, many=True)
    return Response(serializer.data)

@api_view(['GET'])
@skip_middleware_user_activity
@log_request_response
def get_tag_by_index(request, index):
    try:
        tag = Tag.objects.get(index=index)
        serializer = TagSerializer(tag)
        return Response(serializer.data)
    except Tag.DoesNotExist:
        return Response({"error": "Tag not found."}, status=status.HTTP_404_NOT_FOUND)
